﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Warranty.Core.IService;
using Warranty.Core.Domain;
using FX.Core;
using Warranty.Frm.ModelView;

namespace Warranty.Frm
{
    public partial class KhoHang : Form
    {
        private readonly IStaffService _StaffSrv;
        private readonly IStockService _StockSrv;
        private readonly IProductService _ProductSrv;
        List<StockView> rs;
        public KhoHang()
        {
            InitializeComponent();
            _StaffSrv = IoC.Resolve<IStaffService>();
            _ProductSrv = IoC.Resolve<IProductService>();
            _StockSrv = IoC.Resolve<IStockService>();

            dgvKho.AutoGenerateColumns = false;

            DataGridViewTextBoxColumn makeColumn = new DataGridViewTextBoxColumn();
            makeColumn.DataPropertyName = "ID";
            makeColumn.Visible = false;

            DataGridViewTextBoxColumn modelColumn1 = new DataGridViewTextBoxColumn();
            modelColumn1.DataPropertyName = "Name";
            modelColumn1.HeaderText = "Tên";

            DataGridViewTextBoxColumn modelColumn2 = new DataGridViewTextBoxColumn();
            modelColumn2.DataPropertyName = "Price";
            modelColumn2.HeaderText = "Giá";

            DataGridViewTextBoxColumn modelColumn = new DataGridViewTextBoxColumn();
            modelColumn.DataPropertyName = "Quantity";
            modelColumn.HeaderText = "Số lượng";

            dgvKho.Columns.Add(makeColumn);
            dgvKho.Columns.Add(modelColumn1);
            dgvKho.Columns.Add(modelColumn);
            dgvKho.Columns.Add(modelColumn2);
            dgvKho.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvKho.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvKho.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgvKho.ReadOnly = true;
        }

        private void KhoHang_Load(object sender, EventArgs e)
        {
            KhoHangLoad();

        }
        public void KhoHangLoad()
        {

            List<Stock> a = _StockSrv.GetAll().ToList();
            List<Staff> c = _StaffSrv.GetAll().ToList();

            rs = new List<StockView>();
            foreach (var item in a)
            {
                Product b = _ProductSrv.Getbykey(Convert.ToInt32(item.IdProduct));
                StockView sw = new StockView();
                sw.ID = item.ID;
                sw.Quantity = item.Quantity;
                sw.Name = b.Name;
                sw.Price = b.Price;
                rs.Add(sw);

            }

            refreshDataGrid();
        }
        private void refreshDataGrid()
        {
            
            dgvKho.DataSource = typeof(List<>);
            dgvKho.DataSource = rs;
            dgvKho.AutoResizeColumns();
            dgvKho.Refresh();
        }

        private void nhaphangToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NhapHang frmNhap = new NhapHang();
            if (frmNhap.ShowDialog() == DialogResult.OK)
            {
                KhoHangLoad();
            }
        }

        private void xuấtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BanHang frmNhap = new BanHang();
            if (frmNhap.ShowDialog() == DialogResult.OK)
            {
                KhoHangLoad();
            }
        }

        private void nhanBaoHanhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PhieuBH frmNhap = new PhieuBH();
            if (frmNhap.ShowDialog() == DialogResult.OK)
            {
            }
        }

        
       
        
    }
}
